<?php
/**
 * Created by PhpStorm.
 * User: zouyanan
 * Date: 2019/5/11
 * Time: 下午10:35
 */

namespace app\userapi\controller;
use app\common\controller\Api;

class House extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';
    protected $websiteUrl = "http://120.77.150.65/";

    //全部房源
    public function houseAll(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $list = db('yjy_house a')
            ->join("yjy_house_layout b", "a.layout_id = b.id", "LEFT")
            ->field("a.id,a.title,a.image,a.location_near,a.price,b.name as layout")
            ->where("a.house_status",1)
            ->select();
        //foreach($list as $key=>$val){
            //$list[$key]['image'] = $this->websiteUrl.$val['image'];
     //   }
        $this->success("返回成功",$list);
    }

    //地图搜索
    public function mapSearch(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $house_id = $this->request->request("house_id");
        $info = db("yjy_house")->where("Id",$house_id)->field("id,title,location_near,image,images,price")->select();
        $this->success("返回成功",$info);
    }

    //地图标记
    public function markers(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
        $list = db("yjy_house")->field("id,location_near,lon,lat")->select();
        $arr = [];
        foreach($list as $key=>$val){
            $arr[$key]['id'] = $val['id'];
            $arr[$key]['width'] = 40;
            $arr[$key]['height'] = 40;
            $arr[$key]['latitude'] = $val['lat'];
            $arr[$key]['longitude'] = $val['lon'];
            $arr[$key]['iconPath'] = '/static/icon/location.png';
            $arr2['content'] =  $val['location_near'];
            $arr2['color'] = '#00BFFF';
            $arr2['fontSize'] = 10;
            $arr2['borderRadius'] = 4;
            $arr2['borderWidth'] = 1;
            $arr2['borderColor'] = '#333300';
            $arr2['bgColor'] = '#CCFF99';
            $arr2['padding'] = '5';
            $arr2['display'] = 'BYCLICK';
            $arr[$key]['callout'] = $arr2;
        }
        $this->success("返回成功",$arr);
    }



    //全部房源
    public function houseList(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        ob_clean();
       // $action = $this->request->request("action");
        $layout = $this->request->request("layout");
        $area_id = $this->request->request("area_id");
        $price = $this->request->request("price");
        $sort = $this->request->request("sort");
        $keywords = $this->request->request("keywords");
        $page = $this->request->request("page");
        $where = [];
        if($layout){
            $where['a.layout_id'] = $layout;
        }
        if($area_id){
            $where['a.area_id'] = $area_id;
        }
        if($price){
            if($price == 1){
                $where['a.price'] = array('between','1000,2000');
            }
            if($price == 2){
                $where['a.price'] = array("between",'2000,3000');
            }
            if($price == 3){
                $where['a.price'] = array("between",'3000,4000');
            }
            if($price == 4){
                $where['a.price'] = array("between",'9000,10000');
            }
            if($price == 5){
                $where['a.price'] = array("gt",'10000');
            }
        }
        if($sort == 'addtime'){
            $order = array("createtime"=>'asc');
        }elseif($sort == 'low'){
            $order = array("price"=>'asc');
        }elseif($sort == 'high'){
            $order = array("price"=>'desc');
        }else{
            $order = array("id"=>'desc');
        }

      /* if($keywords){
            $list = db('yjy_house a')
                ->join("yjy_house_layout b", "a.layout_id = b.id", "LEFT")
                ->field("a.id,a.title,a.image,a.location_near,a.price,b.name as layout")
                ->where('title', 'like', '%'.$keywords.'%')
                ->paginate(6);
        }else{
      */
            $list = db('yjy_house a')
                ->join("yjy_house_layout b", "a.layout_id = b.id", "LEFT")
                ->field("a.id,a.title,a.image,a.images,a.location_near,a.price,b.name as layout")
                ->where('title', 'like', '%'.$keywords.'%')
                ->where($where)
                ->order($order)
                ->where("a.house_status",1)
                ->paginate(6);



        $this->success("返回成功",$list);
    }


    //户型
    public function layout(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $list_layout = db("yjy_house_layout")->field("*")->select();
        $layout = [];
        foreach($list_layout as $key=>$val){
            $layout[$key]['title'] = $val['name'];
            $layout[$key]['value'] = $val['id'];
        }

        $list_area = db("yjy_house_area")->field("*")->select();
        $area = [];
        foreach($list_area as $key=>$val){
            $area[$key]['title'] = $val['name'];
            $area[$key]['value'] = $val['Id'];
        }
        $data= [
            'layout' => $layout,
            'area'   => $area
        ];

        $this->success("返回成功",$data);
    }



    /*
     * 房源详情
     */
    public function detail(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $house_id = $this->request->request("house_id");

        $house_status = db("yjy_house")->where("Id",$house_id)->value('house_status');
        if($house_status ==2){
            $this->error("该房源已下架",'',1000);
        }
        $yjy_view = db("yjy_view");
        //添加浏览记录
        $agent_id = db("yjy_house")->where("id",$house_id)->value("user_id");
        //判断是否浏览过
        $viewId = $yjy_view->where("user_id",$userInfo['id'])->where("house_id",$house_id)->value("id");
        if(!$viewId){
            $yjy_view->insert([
                'user_id'    => $userInfo['id'],
                'house_id'   => $house_id,
                'agent_id'   => $agent_id,
                'createtime' => time()
            ]);
            db('user')->where('id',$userInfo['id'])->setInc('num_view',1);
        }
        $info = db("yjy_house a")
            ->join('user b','a.user_id = b.id','LEFT')
           // ->join('user_agent c','b.id = c.user_id','LEFT')
            ->join("yjy_house_layout d","d.id = a.layout_id","LEFT")
            ->join('yjy_house_orientations e','e.id=a.orientations_id','LEFT')
            ->field("a.*,b.id as agent_id,b.nickname,b.avatar,b.mobile,b.fans,d.name as layout,e.name as house_orientations,a.video")
            ->field("a.lon,a.lat,a.location_near,a.location_map")
            ->where("a.id",$house_id)
            ->find();

        //判断关注:0否,1是
        $is_attention = db('yjy_attention')
            ->where('user_id',$userInfo['id'])
            ->where('agent_id',$info['agent_id'])
            ->value('id');
        if($is_attention){
            $info['is_attention'] = 1;
        }else{
            $info['is_attention'] = 0;
        }

        //判断是否收藏:0否，1是
        $is_collect = db("yjy_collect")
            ->where("house_id",$house_id)
            ->where("user_id",$userInfo['id'])
            ->value('id');
        if($is_collect){
            $info['is_collect'] = 1;
        }else{
            $info['is_collect'] = 0;
        }
        //顶部图片信息
        $images_info = explode(",",$info['images']);
        $topImage = [];
        foreach($images_info as $key=>$val){
            $topImage[$key]['image'] = $val;
            $topImage[$key]['type'] = 'img';
        }
        if($info['video']){
            array_push($topImage,['type'=>'video','image'=>'/uploads/20200115/c10f4a694b634de851182d8447cf6395.jpg']);
        }

        //房源标签信息
        $arr = [];
        $tagsInfo = db("yjy_house_tags")->select();
        foreach($tagsInfo as $key=>$val){
            $arr[$val['id']] = $val['name'];
        }
        $arr_tags = [];
        $tags_ids = explode(",",$info['tags_ids']);
        foreach($tags_ids as $v){
            array_push($arr_tags,$arr[$v]);
        }
        //房间配置
        $configInfo = db("yjy_house_config")->select();
        $arr_name = [];
        $arr_image = [];
        foreach($configInfo as $key=>$val){
            $arr_name[$val['id']] = $val['name'];
            $arr_image[$val['id']] = $val['image'];
        }
        $arr_config = [];
        $config_ids = explode(",",$info['config_ids']);
        foreach($config_ids as $k=>$v){
            $arr_config[$k]['name'] = $arr_name[$v];
            $arr_config[$k]['image'] = $arr_image[$v];
        }

        //经纪人名下房源信息
        $houseList_agent = db('yjy_house')
            ->field('Id,image,title,location_near,price,images')
            ->where('user_id',$info['agent_id'])
            ->where("house_status",1)
            ->where("Id",'neq',$house_id)
            ->select();
        $data = [
            'images_info'     => $topImage,
            'house_info'      => $info,
            'tags_info' => $arr_tags,
            'config_info' => $arr_config,
            'houseList_agent' => $houseList_agent
        ];
        $this->success("返回成功",$data);

    }


    /**
     * 收藏/取消收藏
     */
    public function collect(){
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $userInfo = $this->auth->getUserinfo();
        $house_id = $this->request->request("house_id");
        $action =  $this->request->request("action");  //添加收藏=add,取消收藏=cancel
        if(!$action || !$house_id){
            $this->error("缺少参数");
        }
        if($action == 'add'){

            //$collect_info = db("yjy_collect")->where("user_id",$userInfo['id'])->where("house_id",$house_id)->find();

            $res_a = db('yjy_collect')->insert([
                'user_id'  => $userInfo['id'],
                'house_id' => $house_id,
                'createtime'=> time()
            ]);
            db("yjy_house")->where("Id",$house_id)->setInc("collect",1);
            $res_b = db('user')->where('id',$userInfo['id'])->setInc('num_collect',1);
            $msg = '收藏';


        }elseif($action == 'cancel'){
            $res_a = db('yjy_collect')->where('user_id',$userInfo['id'])->where('house_id',$house_id)->delete();
            $res_b = db('user')->where('id',$userInfo['id'])->setDec('num_collect',1);
            $msg = '收藏取消';
        }
        if($res_a && $res_b){
            $this->success($msg."成功");
        }else{
            $this->error($msg."失败");
        }
    }







































}